<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >处理</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="showRepairManage"
          >查看需要维修设备</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="repairList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备编号" align="center" prop="deviceCode" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备位置" align="center" prop="deviceLocation" />
      <el-table-column
        label="设备状态"
        align="center"
        prop="deviceStatusText"
      />
      <el-table-column
        label="维修进度"
        align="center"
        prop="repairStatusText"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >处理</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号" prop="deviceCode">
          <el-input
            readonly
            v-model="form.deviceCode"
          />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input readonly v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item readonly label="设备位置" prop="deviceLocation">
          <el-input
          readonly
            v-model="form.deviceLocation"
            placeholder="请输入设备位置"
          />
        </el-form-item>
        <el-form-item label="图片" prop="picPath">
          <image-preview :src="form.picPath" :width="100" />
        </el-form-item>
        <el-form-item label="损坏描述" prop="remark">
          <el-input readonly v-model="form.remark" placeholder="请输入损坏描述" />
        </el-form-item>
        <el-form-item label="维修进度" prop="repairStatus">
          <el-select readonly v-model="form.repairStatus" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修人员" prop="repairUser">
          <el-select v-model="form.repairUser" placeholder="请选择">
            <el-option
              v-for="item in repairUserList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="需要维修的设备" :visible.sync="open2" width="800px" append-to-body>
      <el-table
      v-loading="loading"
      :data="repairManages"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备位置" align="center" prop="deviceLocation" />
      <el-table-column
        label="设备状态"
        align="center"
        prop="deviceStatusText"
      />
      <el-table-column
        label="维修进度"
        align="center"
        prop="repairStatusText"
      />
    </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  manageList,
  getRepair,
  delRepair,
  updateRepair,
  getRepairUser,
  getrepair,
} from "./repair";
import request from "@/utils/request";

export default {
  name: "Repair",
  data() {
    return {
      repairManages:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修管理表格数据
      repairList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: null,
        deviceStatus: null,
        repairStatus: null,
      },
      options: [
        {
          label: "驳回",
          value: "1",
        },
        {
          label: "未受理",
          value: "2",
        },
        {
          label: "已受理",
          value: "3",
        },
        {
          label: "完成",
          value: "4",
        },
      ],
      repairUserList: [],
      dialogImageUrl: '',
      dialogVisible: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    log(){
      // console.log(this.form.repairUser)
    },
    /** 查询维修管理列表 */
    getList() {
      this.loading = true;
      this.repairUserList=[],
      getRepairUser().then((response) => {
        for (let i = 0; i < response.length; i++) {
          const item = response[i];
          // 检查当前元素是否包含 userId 和 userName 属性
          if (item.userId !== undefined && item.userName !== undefined) {
            // 创建一个新对象，包含 userId 和 userName 属性
            const userInfo = {
              value: String(item.userId),
              label: item.nickName,
            };
            // 将新对象添加到 repairUserList 数组中
            this.repairUserList.push(userInfo);
          }
        }
      });
      // console.log(this.repairUserList);
      manageList(this.queryParams).then((response) => {
        this.repairList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleRemove(file, fileList) {
      // console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.form.picPath = file.url;
      this.dialogVisible = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel2() {
      this.open2 = false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceName: null,
        deviceLocation: null,
        picPath: null,
        deviceId: null,
        deviceStatus: null,
        remark: null,
        deviceStatusText: null,
        repairStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getRepair(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "分配维修人员";
      });
    },
    showRepairManage(){
      this.open2 = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateRepair(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRepair(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除此条报修记录？')
        .then(function () {
          return delRepair(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "hotel/repair/export",
        {
          ...this.queryParams,
        },
        `repair_${new Date().getTime()}.xlsx`
      );
    },
  },
  mounted() {
    getrepair().then((res) => {
          this.repairManages = res
        })
        .catch(() => {});
  },
};
</script>
